-- Dissector for rtp payload (demo)
-- Author: Huang Qiangxiong (qiangxiong.huang@gmail.com)
-- change log:
--      2010-09-12
--          Just can play
------------------------------------------------------------------------------------------------
-- Author: Aki Kaede
-- change log:
--      2014-03-17
--          Add support for RTP Redundancy
--          you can use it combine with h264_export.lua
------------------------------------------------------------------------------------------------

do
  local h264_dis = Dissector.get("h264")

  local s264_proto = Proto("s264","S.264")
  local s264_f_pt = ProtoField.uint8("s264.pt","Payload type",base.DEC,
    {[98]= "H263-1988",[99]="H264",[97]="H264"})

  s264_proto.fields={s264_f_pt}

  local prefs = s264_proto.prefs
  prefs.dyn_pt = Pref.uint("S264 dynamic payload type", 116, "The value > 95")

  function s264_proto.dissector(tvb,pinfo,tree)
    pinfo.cols.protocol = "S264"
    local subtree = tree:add(s264_proto,tvb())
    subtree:add(s264_f_pt,tvb(0,1))
    h264_dis:call(tvb(1):tvb(),pinfo,tree)
  end

  -- register this dissector to dynamic payload type dissectorTable
  local dyn_payload_type_table = DissectorTable.get("rtp_dyn_payload_type")
  dyn_payload_type_table:add("MYRTP", s264_proto)

  -- register this dissector to specific payload type (specified in preferences windows)
  local payload_type_table = DissectorTable.get("rtp.pt")
  local old_dissector = nil
  local old_dyn_pt = 0
  function s264_proto.init()
    if (prefs.dyn_pt ~= old_dyn_pt) then
      if (old_dyn_pt > 0) then -- reset old dissector
        if (old_dissector == nil) then -- just remove this proto
          payload_type_table:remove(old_dyn_pt, s264_proto)
      else  -- replace this proto with old proto on old payload type
        payload_type_table:add(old_dyn_pt, old_dissector)
      end
      end
      old_dyn_pt = prefs.dyn_pt  -- save current payload type's dissector
      old_dissector = payload_type_table:get_dissector(old_dyn_pt)
      if (prefs.dyn_pt > 0) then
        payload_type_table:add(prefs.dyn_pt, s264_proto)
      end
    end
  end
end